<template>
  <view>
      <view class="goods-list">
        <block v-for="(goods, i) in goodsList" :key="i">
          <my-goods :goods="goods"></my-goods>
        </block>
      </view>
    </view>
</template>

<script>
  import {throttle} from 'lodash'
export default {
  data() {
    return {
      queryObj: {
        query: '', //查询关键词
        cid: '', //分类id
        pagenum: 1, //页数索引
        pagesize: 20 //每页长度
      },
      // 商品列表
      goodsList: [],
      // 总数量
      total: 0,
    };
  },
  onLoad(options) {
    this.queryObj = { ...this.queryObj, ...options };
    this.getGoodsList();
  },
  onPullDownRefresh() {
    // 恢复数据
    this.goodsList = []
    this.queryObj.pagenum = 1
    this.total = 0
    // 重新获取数据
    this.getGoodsList()
    // 关闭下拉
    uni.stopPullDownRefresh()
  },
  onReachBottom:throttle(function() {
    this.queryObj.pagenum++
    this.getGoodsList();
  },2000),
  methods: {
    async getGoodsList() {
      if(this.goodsList.length == this.total && this.total!=0) return 
      let { data: res } = await uni.$http.get('/goods/search', this.queryObj);
      if (res.meta.status !== 200) return uni.$showMsg();
      this.goodsList = [...this.goodsList,...res.message.goods];
      this.total = res.message.total;
      // console.log(this.goodsList);
    }
  }
};
</script>

<style lang="scss">

</style>
